package com.crm.controller.repositoryController;

import com.crm.exception.ServiceException;

import com.crm.pojo.Repository;
import com.crm.service.repositoryService.RepositoryService;
import com.crm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class RepositoryController {
    @Autowired
    private RepositoryService repositoryService;

    //打开仓库管理选项卡
    @GetMapping("repositoryM")
    public String toProRepository(){
        return "repository/repository";
    }
    //打开新增仓库表单
    @GetMapping("to-form-repository-add")
    public String toFormRepositoryAdd(){
        return "repository/newRepository";
    }
    //打开修改仓库表单
    @GetMapping("to-form-repository-update")
    public String toFormRepositoryUpdate(){
        return "repository/editRepository";
    }
    //新增表单提交
    @PostMapping("newRepository-form-sub")
    @ResponseBody
    public Result newRepository(Repository repository){
        try{
            repositoryService.save(repository);
            return Result.SUCCESS;
        }catch(ServiceException e){
            e.printStackTrace();
            return new Result("新增失败！");
        }
    }
    //分页查询所有的仓库
    @PostMapping("allRepositoryPaged")
    @ResponseBody
    public Map allRepositoryPaged(int page, int rows) {
        Long total = repositoryService.queryCount();
        List<Repository> re = repositoryService.allRepositoryPaged(page, rows);
        HashMap map = new HashMap<>();
        map.put("total", total);
        map.put("rows", re);
        return map;
    }
    //模糊查询仓库根据仓库名称和仓库地址
    @PostMapping("queryByrnameOrraddressMh")
    @ResponseBody
    public List<Repository> queryByrnameOrraddressMh(String q){
        return repositoryService.allRepositoryByMh(q);
    }
    //后台重新获取对应rid的仓库信息
    @GetMapping("getRowData4")
    @ResponseBody
    public Repository getRowData4(Long rid){
        return repositoryService.findOne(rid);
    }
    //修改表单提交
    @PostMapping("editRepository-form-sub")
    @ResponseBody
    public Result editRepository(Repository repository){
        try{
            repositoryService.save(repository);
            return Result.SUCCESS;
        }catch(ServiceException e){
            e.printStackTrace();
            return new Result("修改失败！");
        }
    }
    //批量或单删
    //逻辑删除仓库(需要仓库中无出入库，即仓库中无产品)
    @PostMapping("repository-del")
    @ResponseBody
    public Result repositoryDel(String rowsId){
        try {
            String[] idsS = rowsId.split(",");
            Long[] ids = new Long[idsS.length];
            for (int i = 0; i < idsS.length; i++) {
                ids[i] = Long.parseLong(idsS[i]);
                //查找对应仓库下的仓库出入库单数量
                Long count = repositoryService.findCount(ids[i]);
                if(count!=0L){
                   return new Result("无法删除仓库！仓库中有未转移的出入库流水！");
                }else{
                    //删除对应rid的仓库
                    repositoryService.deleteR(ids[i]);
                }
            }
            return Result.SUCCESS;
        } catch (ServiceException e) {
            e.printStackTrace();
            return new Result("删除失败！");
        }
    }
}
